﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KVB.Common;
using Customer_Profile.eCard.DBDao;
using Customer_Profile.Admin;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace Customer_Profile.eCard.CardAccount
{
    public partial class ExpenseCheckList : System.Web.UI.Page
    {
        string strUserName = ""; 
        string strUserID = "";
        string strEntityID = "";
        string strEntity = "";
        string sumExpenseAmount = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Comm.CheckSession(this.Page, "/login.aspx", "usermodel");
            if (Session["usermodel"] != null)
            {
                strUserID = ((UserInfoEntity)Session["usermodel"]).UserLoginId;
                strUserName = ((UserInfoEntity)Session["usermodel"]).UserName;
                strEntityID = ((UserInfoEntity)Session["usermodel"]).UserEntityID;
                strEntity = ((UserInfoEntity)Session["usermodel"]).UserOffice;
            }
            else
            {
                Log.WriteDefaultLog("main_01 Page_Load", "Session is null");
                return;
            }
            bindGrid();
        }

        private void bindGrid()
        {
            try
            {
                string strWhere = "where expense_status='EV' ";
                if (txtCardNo.Text.Trim() != "")
                    strWhere += " and ed.card_no ='" + txtCardNo.Text.Trim() + "'";
                
                if (txtcreatedatebegin.Text != "" && txtcreatedateend.Text != "")
                {
                    strWhere += " and convert(date,ed.input_date) between '" + txtcreatedatebegin.Text.Trim() + "' and '" + txtcreatedateend.Text.Trim() + "'";
                }
                strWhere += " ORDER BY expense_no DESC";
                DataSet ds = AccountingDao.getExpenseDealList(strWhere);
                if (ds != null && ds.Tables.Count > 0)
                {
                    sumExpenseAmount = ds.Tables[0].Compute("sum(amount)", "1>0").ToString();
                    GvData.DataSource = ds.Tables[0];
                    GvData.DataBind();
                }
                else
                {
                    GvData.DataBind();
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("bindGrid", ex.ToString());
                Response.Write(ex.ToString());
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

        }

        protected void GvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GvData.PageIndex = e.NewPageIndex;
            bindGrid();
        }

        protected void GvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    LinkButton lbcardno = (LinkButton)e.Row.Cells[2].Controls[1];
                    lbcardno.Attributes.Add("onclick", "javascript:window.open('/eCard/CardDetail.aspx?cardno=" + lbcardno.Text + "');return false");

                    string strExpenseNo = GvData.DataKeys[e.Row.RowIndex].Values["expense_no"].ToString();
                    string strStatus = GvData.DataKeys[e.Row.RowIndex].Values["expense_status"].ToString();
                    string input_userid = GvData.DataKeys[e.Row.RowIndex].Values["input_user"].ToString();

                    LinkButton lbAction = (LinkButton)e.Row.Cells[9].Controls[1];

                    if (strStatus.Equals("EV"))
                    {
                        lbAction.Text = "审核";                       
                        lbAction.Attributes.Add("onclick", "javascript: var s_height = screen.height - 75; window.open('ExpenseCheck.aspx?expense_no=" + strExpenseNo + "', 'newwindow2', 'width=' + screen.width + ',height=' +  s_height + ',top=0,left=0,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');return false");                        
                    }
                    else
                    {
                        lbAction.Text = "";
                    }
                }

                if (e.Row.RowType == DataControlRowType.Footer)
                {
                    e.Row.Cells[0].Text = "合计";
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.White;
                    e.Row.Cells[5].Text = sumExpenseAmount;
                    e.Row.Cells[5].ForeColor = System.Drawing.Color.White;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            //标题
            byte[] bTitle = System.Text.Encoding.GetEncoding("UTF-8").GetBytes("交易信息" + "<br><br>");
            StringBuilder sb = new StringBuilder();
            sb.Append(System.Text.Encoding.GetEncoding("UTF-8").GetString(bTitle));

            //报表内容
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            GvData.AllowPaging = false;
            GvData.ShowHeader = true;
            bindGrid();
            GvData.RenderControl(oHtmlTextWriter);
            byte[] bContent = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(oStringWriter.ToString());
            sb.Append(System.Text.Encoding.GetEncoding("UTF-8").GetString(bContent));

            //输出
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.Buffer = true;
            System.Web.HttpContext.Current.Response.Charset = "UTF-8";
            System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=mmdeals.xls");
            System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            System.Web.HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";    //设置输出文件类型为excel文件。 
            System.Web.HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=" + "UTF-8" + "\" >");

            string strExcel = sb.ToString();

            //去除超链接 
            strExcel = Regex.Replace(strExcel, "<a (.*?)>", "", RegexOptions.Compiled);
            strExcel = Regex.Replace(strExcel, "</a>", "");

            System.Web.HttpContext.Current.Response.Write(strExcel);
            System.Web.HttpContext.Current.Response.End();

        }

        public override void VerifyRenderingInServerForm(Control control)
        {
        }
    }
}